//
//  PieVIew.m
//  07-柱状图
//
//  Created by J on 16/2/23.
//  Copyright © 2016年 J. All rights reserved.
//

#import "PieVIew.h"

@implementation PieVIew


- (NSArray*)arrRandom{
    
    int total = 100;
    int temp = 0;
    NSMutableArray* arrMutable = [NSMutableArray array];
    for (int i=0; i<3; i++) {
        temp = arc4random_uniform(total) + 1 ;//0-99
        [arrMutable addObject:@(temp)];
        total -=temp;//从剩下的里面随机不然就超了
        if (temp == total) {//如果第1次就随机一般 直接跳出循环
            break;
        }
    }
    
    if (total) {//如果没有随机完总数100 还有剩余的直接加到数组
        [arrMutable addObject:@(total)];
    }
    return arrMutable;
}

- (void)drawRect:(CGRect)rect {
    // 饼图
    NSArray* arr= [self arrRandom];
    CGFloat radi = self.bounds.size.width*0.5;
    CGPoint center= CGPointMake(radi, radi);
    
    

    CGFloat startA = 0;
    CGFloat angle = 0;
    CGFloat endA = 0;
    
    

    
    
    
    for (NSNumber* num in arr) {
        startA = endA;
        angle = [num floatValue]/100.0*M_PI*2;
        endA = startA + angle;
        
        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radi startAngle:startA endAngle:endA clockwise:YES];
        //添加一条线到圆形
        [path addLineToPoint:center];//从起点自动添加一条线到中点
        [[self colorRandom]set];//set 描边和填充通用
        [path fill];//自动封闭路径但是并不会添加一条线 下一个饼状图的开始 开始得自己画一根线补上才能在下一个饼状图结束的时候调用这个方法形成封闭空间
    }
    
    
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self setNeedsDisplay];
}

- (UIColor*)colorRandom{
    CGFloat r = arc4random_uniform(256)/255.0;
    CGFloat g = arc4random_uniform(256)/255.0;
    CGFloat b = arc4random_uniform(256)/255.0;

    return [UIColor colorWithRed:r green:g blue:b alpha:1.0];
    
}

- (void)draw{
    // 饼图
    
//    NSArray* arr= @[@25,@25,@25];
    CGFloat radi = self.bounds.size.width*0.5;
    CGPoint center= CGPointMake(radi, radi);
    
    
    
    CGFloat angle = 0;
    CGFloat starA = 0;
    CGFloat endA = 25/100.0*M_PI*2;;
    
    
    
    angle = 25/100.0*M_PI*2;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radi startAngle:starA endAngle:endA clockwise:YES];
    //添加一条线到圆形
    [path addLineToPoint:center];//从起点自动添加一条线到中点
    [[UIColor orangeColor]set];//set 描边和填充通用
    [path fill];//自动封闭路径但是并不会添加一条线 下一个饼状图的开始 开始得自己画一根线补上才能在下一个饼状图结束的时候调用这个方法形成封闭空间
    
    
    
    
    
    
    
    
    starA = endA;
    angle = 25/100.0*M_PI*2;
    endA = starA + angle;
    UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:center radius:radi startAngle:starA endAngle:endA clockwise:YES];
    //添加一条线到圆形
    [path2 addLineToPoint:center];
    [[UIColor purpleColor]set];//set 描边和填充通用
    [path2 fill];
    
    
    
    starA = endA;
    angle = 50/100.0*M_PI*2;
    endA = starA + angle;
    UIBezierPath *path3 = [UIBezierPath bezierPathWithArcCenter:center radius:radi startAngle:starA endAngle:endA clockwise:YES];
    //添加一条线到圆形
    [path3 addLineToPoint:center];
    [[UIColor brownColor]set];//set 描边和填充通用
    [path3 fill];
    
}


@end
